import { getTokenKey } from "../../utils/auth";

const app = getApp();
Component({
  properties: {
    userInfo: {
      type: Object,
      default: null
    },
    selfType: {
      type: String,
      default: '普通用户'
    }
  },
  data: {
    userType:'',
    avatarUrl: '../../images/album.png'
  },
  lifetimes: {
    //执行一次
    attached: function() {
      console.log("组件进入")
      this.getUserType()
    },

  },
  methods: {
    getUserType(){
      if (this.data.userInfo){
        console
        switch(this.data.userInfo.roles.length){
          case 4:
            this.setData({
              userType: '超级管理员'
            })
            break;
          case 3:
            this.setData({
              userType: '队站管理员'
            })
            break;
          case 2:
            this.setData({
              userType: '管理员'
            })
            break;
          case 1:
            this.setData({
              userType: '普通用户'
            })
            break;
        }
      }
    },
    accept(){
      const data ={
        roles: [{'name': 'ROLE_USER'},{'name': 'ROLE_ADMIN'}],
        applyAdmin: false 
      }
      this.updateBasicInfo(data).then(res => {
        console.log(res)
        if(res.id){
          this.setData({
            userInfo: res,
            userType:'管理员'
          })
        }
      })
    },
    reject() {
      const data ={
        applyAdmin: false 
      }
      this.updateBasicInfo(data).then(res => {
        console.log(res)
        if(res.id){
          this.setData({
            userInfo: res,
            userType:'普通用户'
          })
        }
      })
    },

    // 更新用户信息 绑定图像
    updateBasicInfo(data){
      return new Promise(resolve=>{
        resolve(
          app.call({
            path:`/users/${this.data.userInfo.id}`,
            method: 'put',
            header: {
                'X-WX-SERVICE': 'fire-assistant-s',
                'tokens': getTokenKey()//必须添加#######
              },
            data:data
          })
        )
      })
    },

  }
})
